<template>
  <div class="member-order-page">
    <XtxTabs v-model="activeName" @on-click="onClick"> 
      <XtxTabsPanel v-for="item in orderStatus" :key="item.name" :name="item.name" :label="item.label">{{item.label}}</XtxTabsPanel>
    </XtxTabs>
  </div>
   <!-- 订单列表 -->
   <div class="order-list" v-if="orderList">
      <div class="order-item" v-for="item in orderList" :key="item.id">
        <div class="head">
          <span>下单时间：{{item.createTime}}</span>
          <span>订单编号：{{item.id}}</span>
          <span class="down-time" v-if="[1].includes(item.orderState)">
            <i class="iconfont icon-down-time"></i>
            <b>付款截止：{{item.payLatestTime}}</b>
          </span>
          <a class="del" href="javascript:;" v-if="[6].includes(item.orderState)" @click="deleteOrder(item)">删除</a>
        </div>
        <div class="body">
          <div class="column goods">
            <ul>
              <li v-for="i in item.skus" :key="i.id">
                <a class="image" href="javascript:;">
                  <img :src="i.image" alt="" />
                </a>
                <div class="info">
                  <p class="name ellipsis-2">{{i.name}}</p>
                  <p class="attr ellipsis">
                    <span>{{i.properties.propertyMainName}}:{{i.properties.propertyValueName}}</span>
                    <span>尺寸：{{i.attrsText}}</span>
                  </p>
                </div>
                <div class="price">¥{{i.realPay}}</div>
                <div class="count">x{{i.quantity}}</div>
              </li>
            </ul>
          </div>
          <div class="column state" v-if="[1].includes(item.orderState)">
            <p>待付款</p>
          </div>
          <div class="column state" v-if="[1].includes(item.orderState)">
            <p>待发货</p>
          </div>
          <div class="column state" v-if="[1].includes(item.orderState)">
            <p>待收货</p>
            <a href="javascript:;" @click="looklogisticsFn(item)">查看物流</a>
          </div>
          <div class="column amount">
            <p class="red">¥{{item.totalMoney}}</p>
            <p>（含运费：¥{{item.postFee}}）</p>
            <p>在线支付</p>
          </div>
          <div class="column action">
            <XtxButton type="primary" size="small" @click="confirmOrderFn(item)" v-if="[3].includes(item.orderState)">确认收货</XtxButton>
            <XtxButton type="primary" size="small" @click="PayOrderFn(item)" v-if="[1].includes(item.orderState)">立即付款</XtxButton>
            <XtxButton type="primary" size="small"  v-if="[4].includes(item.orderState)">立即评价</XtxButton>
            <p><a href="javascript:;" @click="getOrderList()">查看详情</a></p>
            <p><a href="javascript:;" @click="userOrderCancel(item)">取消订单</a></p>
            <!-- <Teleport to="#dailog"> -->
            <!-- </Teleport> -->
            <Teleport to="#dailog">
              <LogisticOrder ref="logisticsOrderCom"/>
            </Teleport>
          </div>
        </div>
      </div>
      <div v-if="loading" class="loading"></div>
      <div class="none" v-if="!loading && orderList.length===0">暂无数据</div>
    </div>
    <OrderCancel ref="orderCancelCom" />
    <div class="order-detail-page" v-if="order">
      <!-- 操作栏 -->
      <DetailAction :order="order" />
      <!-- 步骤条 组件xtx-steps.vue-->
      <!-- 物流栏 -->
      <!-- 订单商品信息 -->
    </div>
     <!-- 分页组件 -->
     <XtxPagination v-if="counts>reqParams.pageSize" @change-Page="reqParams.page=$event" :counts="counts" :pageSize="reqParams.pageSize" :currCounts="reqParams.orderState"/>
</template>

<script>
import DetailAction from './components/detailAction.vue'
import LogisticOrder from './components/orderLogistic.vue'
import { reactive, ref, watch } from 'vue'
import { orderStatus } from '@/api/contans'   
import { CancelOrder, delOrder, findOrderList, confirmOrder, getOrderDetail } from '@/api/order'
import OrderCancel from '@/views/member/order/components/orderCancel.vue'
import Message from '@/components/library/Message'
import { useRoute } from 'vue-router'
export default {
    name:'MemberOrder',
    components:{OrderCancel, LogisticOrder, DetailAction},
    setup(){
      const activeName = ref('all')
      // tab切换与加载组件 
      const loading = ref(true)
      const onClick = (active) => {
        reqParams.orderState = active.index
        console.log(reqParams.orderState)
        reqParams.page = 1
      }
      // 订单列表
      const reqParams = reactive({
        page:1,
        pageSize:3,
        orderState:0
      })
      const orderList = ref([])
      // 分页实现
      const counts = ref(0)
      const findOrderListFn = () => {
        loading.value = true
        findOrderList(reqParams).then(data=>{
        orderList.value = data.result.items
        loading.value = false
        counts.value = data.result.counts
      })
      }
      watch(reqParams,()=>{
        findOrderListFn()
      },{immediate:true})
      const orderCancelCom = ref(null)
      const userOrderCancel = (item) => {
        orderCancelCom.value.open(item)   
      }
      //  删除订单
      const delOrderGoods = ref(null)
      const deleteOrder = (item) => {
        delOrder([item.id]).then(()=>{
          Message({type:'success',text:'删除成功'})
          findOrderListFn()
        })
      }
      // 确认收货
      const confirmOrderFn = (item) => {
        confirmOrder(item.id).then(()=>{
            Message({type:'success',text:'确认收货成功'})
            item.orderState = 4
        })
      }
      // 查看物流
      const logisticsOrderCom = ref(null)
      const looklogisticsFn = (item) => {
        // console.log(item,'item11111')
        // logisticsOrderCom.value.open(item)
      }
      // 头部展示
      const order = ref(null)
      const route = useRoute()
      const orderId = route.query.id
      // const getOrderList = () => {
        // getOrderDetail(route.query.id).then(data=>{
        //   order.value = data.result
        //   console.log(data.result,'data.result66666')
        // })
      // }
      
      
      
      return {  order, looklogisticsFn, logisticsOrderCom, confirmOrderFn, findOrderListFn, delOrderGoods, deleteOrder, orderStatus, activeName, onClick, orderList, loading, counts, reqParams, orderCancelCom,userOrderCancel }
    }
}
</script>、

<style lang="less">
.order-list {
  background: #fff;
  padding: 20px;
  height:1020px;
}
.order-item {
  margin-bottom: 20px;
  border: 1px solid #f5f5f5;
  .head {
    height: 50px;
    line-height: 50px;
    background: #f5f5f5;
    padding: 0 20px;
    overflow: hidden;
    span {
      margin-right: 20px;
      &.down-time {
        margin-right: 0;
        float: right;
        i {
          vertical-align: middle;
          margin-right: 3px;
        }
        b {
          vertical-align: middle;
          font-weight: normal;
        }
      }
    }
    .del {
      margin-right: 0;
      float: right;
      color: #999;
    }
  }
  .body {
    display: flex;
    align-items: stretch;
    .column {
      border-left: 1px solid #f5f5f5;
      text-align: center;
      padding: 20px;
      > p {
        padding-top: 10px;
      }
      &:first-child {
        border-left: none;
      }
      &.goods {
        flex: 1;
        padding: 0;
        align-self: center;
        ul {
          li {
            border-bottom: 1px solid #f5f5f5;
            padding: 10px;
            display: flex;
            &:last-child {
              border-bottom: none;
            }
            .image {
              width: 70px;
              height: 70px;
              border: 1px solid #f5f5f5;
            }
            .info {
              width: 220px;
              text-align: left;
              padding: 0 10px;
              p {
                margin-bottom: 5px;
                &.name {
                  height: 38px;
                }
                &.attr {
                  color: #999;
                  font-size: 12px;
                  span {
                    margin-right: 5px;
                  }
                }
              }
            }
            .price {
              width: 100px;
            }
            .count {
              width: 80px;
            }
          }
        }
      }
      &.state {
        width: 120px;
        .green {
          color: @xtxColor;
        }
      }
      &.amount {
        width: 200px;
        .red {
          color: @priceColor;
        }
      }
      &.action {
        width: 140px;
        a {
          display: block;
          &:hover {
            color: @xtxColor;
          }
        }
      }
    }
  }
}
.order-list {
  padding: 20px;
  position: relative;
  min-height: 400px;
}
.loading {
  height: 100%;
  width: 100%;
  position: absolute;
  left: 0;
  top: 0;
  background: rgba(255,255,255,.9) url(../../../assets/images/loading.gif) no-repeat center;
}
.none {
  height: 400px;
  text-align: center;
  line-height: 400px;
  color: #999;
}
</style>